<template>
  <div>
    <h1>个人信息</h1>

    <z-form
      ref="zFormRef"
      :schemas="getSchemas()"
      submitActionText="保存"
      @submit="handleSubmit"
    >
    </z-form>
  </div>
</template>

<script setup lang="ts">
import { useAuthStore } from "@/stores/auth";
import { getSchemas } from "./data";
import { ref, onMounted } from "vue";
import { updateSelfInfo } from "@/api/octp/routes/user";

const authStore = useAuthStore();
const user = authStore.getUserInfo();

const zFormRef = ref();
async function handleSubmit(values: any) {
  updateSelfInfo(values);
}

onMounted(() => {
  zFormRef.value.setFields(
    getSchemas().reduce((a, s) => {
      a[s.path] = user[s.path];
      if (s.path === "avatar_id") {
        a[s.path] = user["avatar"].id;
      }
      return a;
    }, {} as any)
  );
});
</script>
